<?php


namespace app\common\service\live;


use app\admin\model\Config;
use app\admin\model\live\Live;
use app\admin\model\live\LiveGoods;
use app\admin\model\live\LiveHost;
use app\admin\model\store\Goods;
use app\admin\model\store\GoodsComment;
use app\common\service\BaseService;
use app\common\service\lib\tencentCloud\CosLive;
use think\facade\Cache;
use think\facade\Log;

class LiveService extends BaseService
{
    protected static $limit = 4;

    /**
     * Author:YangXinPeng
     * 直播推断流回调处理
     * @param $params
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function notifyUrl($params)
    {
        $streamId = $params['channel_id'];
        $appName = $params['appname'];
        $type = $params['event_type'];
        $where = [];
        $where['live_no'] = $streamId;
        $where['app_name'] = $appName;
        $live = Live::where($where)->find();
        $live && $live = $live->toArray();
        $liveId = $live['id'];
        if ($live) {
            if ($type == 1) {
                $startTime = $params['event_time'];
                Live::where('id',$liveId)->update(['status' => 1, 'start_time' => $startTime]);
            } elseif ($type == 0) {
                $endTime = $params['event_time'];
                Live::where('id',$liveId)->update(['status' => 2, 'end_time' => $endTime]);
            }
        } else {
            return Log::info('该直播不存在');
        }
    }

    /**
     * Author:YangXinPeng
     * 获取推流地址
     * @param $streamName
     * @param $year
     * @return string
     */
    public static function getPushUrl($streamName,$year)
    {
        $time = date('Y-m-d H:i:s', $year);
        $domain = Config::beOne('cos.live.push.domain');
        $key = Config::beOne('cos.live.key');
        $instnace = new CosLive();
        $url = $instnace->getPushUrl($domain, $streamName, $key, $time);
        return $url;
    }

    /**
     * Author:YangXinPeng
     * 获取拉流地址
     * @param $streamName
     * @param $year
     * @return string
     */
    public static function getPullUrl($streamName,$year)
    {
        $time = date('Y-m-d H:i:s', $year);
        $domain = Config::beOne('cos.live.pull.domain');
        $key = Config::beOne('cos.live.key');
        $instnace = new CosLive();
        $url = $instnace->getPullUrl($domain, $streamName, $key, $time);
        return $url;
    }

    /**
     * Author:YangXinPeng
     * 下拉主播
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function hosts()
    {
        $data = LiveHost::field('id,name')->select();
        $data && $data = $data->toArray();
        return ['code' => 1, 'msg' => null, 'data' => $data];
    }

    /**
     * Author:YangXinPeng
     * 根据ID查询直播间
     * @param $id
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function byId($id)
    {
        $data = \app\admin\model\live\Live::find($id);
        $data && $data = $data->toArray();
        return $data;
    }

    public static function showById($id)
    {
        $data = \app\admin\model\live\Live::find($id);
        $data && $data = $data->toArray();
        return self::success('成功',$data);
    }


    /**
     * Author:YangXinPeng
     * 同步数据库
     * @param $pullUrl
     * @param $pushUrl
     * @param $id
     */
    public static function saveUrl($pullUrl,$pushUrl,$id)
    {
        Live::where('id',$id)->update([
           'push_url'=>$pushUrl,
            'pull_url'=>$pullUrl
        ]);
    }

    /**
     * Author:Yingbo Sun
     * 直播列表接口
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function liveList()
    {
        $data = Live::where('status','1')->select();
        $data && $data = $data->toArray();
        return self::success('获取成功',$data);
    }

    /**
     * Author:Yingbo Sun
     * 直播商品列表
     * @param $liveId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function liveGoodsList($liveId)
    {
        if (empty($liveId)){
            return self::fail('参数错误');
        }
        $redis = Cache::store('redis');
        $redis->select(13);
        $bool = $redis->has("liveGoodsList:$liveId");
        if ($bool){
            $data = $redis->get("liveGoodsList:$liveId");
        }else{
            $field = ['id','live_id','goods_id','live_price','original_price'];
            $data = LiveGoods::with('liveGoods')->where('live_id',$liveId)
                ->field($field)
                ->select();
            $data && $data = $data->toArray();

            if (empty($data)){
                return self::fail('没有商品了，您和主播聊天吧！');
            }
            $redis->set("liveGoodsList:$liveId",$data);
        }
        return self::success('获取成功',$data);
    }

    /**
     * Author:Yingbo Sun
     * 直播标题接口 查询四个
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function liveLabelList()
    {
        $data = Live::order('start_time','desc')
            ->where('status',1)
            ->limit(self::$limit)->select();
        $data && $data = $data->toArray();
        return self::success('获取成功',$data);
    }

    /**
     * Author:YangXinPeng
     * 后台直播列表展示
     * @param $page
     * @param $limit
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function liveInfo($page,$limit,$where)
    {
        $model = new Live();
        $count = $model->where($where)->count();
        $list = $model->where($where)
            ->with('hosts')
            ->page($page, $limit)
            ->order('create_time','desc')
            ->select();
        if ($list){
            $list = $list->toArray();
        }else{
            $list = [];
        }

        $data = [
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list,
        ];

        return $data;
    }

}